<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="main_crumbs boxdq">
          <div class="main_crumbs_l">
            <a href="javascript:;" @click="$router.back(-1)">
              <i class="el-icon-arrow-down"></i>返回
            </a>
            <em>|</em>
            <span>领取记录</span>
          </div>
        </div>

        <div class="mainor_form marginTop15">
          <div class="mainor_te">
            <el-button class="mainor_tea" @click="exportbuVisible = true"
              >导出</el-button
            >
            <!-- @click="exportOrderiInfo" -->
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="用户" prop="uid">
              <change-user-input
                ref="changeUserInput"
                @sendValue="getUser"
                @clear="clearUid"
              ></change-user-input>
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select
                v-model="form.status"
                class="wid220"
                placeholder="使用状态"
              >
                <el-option label="全部" :value="parseFloat(0)"></el-option>
                <el-option
                  :label="item"
                  :value="parseFloat(index) + 1"
                  v-for="(item, index) in status_list"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="领取时间" prop="order_sn">
              <el-date-picker
                v-model="createTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                :picker-options="pickerOptions"
              />
            </el-form-item>

            <el-form-item>
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="reset"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_table marginTop10" style="min-height: 550px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            @selection-change="handleSelectionChange"
          >
            <!-- :summary-method="getSummaries"
            show-summary -->
            <el-table-column
              label="优惠券名称"
              align="center"
              prop="coupons.title"
              show-overflow-tooltip
              min-width="120"
            />
            <el-table-column
              label="面值"
              align="center"
              prop="money"
              show-overflow-tooltip
              min-width="120"
            />
            <el-table-column
              label="用户"
              align="center"
              prop="member.nickname"
              show-overflow-tooltip
              min-width="120"
            />
            <el-table-column
              label="领取时间"
              align="center"
              prop="name"
              show-overflow-tooltip
              min-width="160"
            >
              <template slot-scope="scope">
                {{ parseTime(Number(scope.row.created_at)) }}
              </template>
            </el-table-column>
            <el-table-column
              label="状态"
              align="center"
              prop="name"
              show-overflow-tooltip
              min-width="120"
            >
              <template slot-scope="scope">
                <span :class="'status' + scope.row.status">
                  {{ status_list[scope.row.status] }}</span
                >
              </template>
            </el-table-column>
            <el-table-column
              label="使用时间"
              align="center"
              prop="name"
              show-overflow-tooltip
              min-width="160"
            >
              <template slot-scope="scope">
                {{
                  scope.row.status == 1
                    ? parseTime(Number(scope.row.use_time))
                    : ""
                }}
              </template>
            </el-table-column>
            <el-table-column
              label="使用订单"
              align="center"
              prop="order_sn"
              show-overflow-tooltip
              min-width="120"
            />
            <el-table-column
              label="操作"
              align="center"
              prop="name"
              show-overflow-tooltip
              min-width="120"
            >
              <template slot-scope="scope">
                <el-link
                  :type="scope.row.status == 0 ? 'danger' : 'info'"
                  @click="handleCancel(scope.row)"
                  >作废</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <!-- 订单导出 -->
    <el-dialog
      title="货到付款账单导出"
      :visible.sync="exportbuVisible"
      class="main_dialog dialog500"
    >
      <p class="exptitle">选择导出时间</p>
      <el-date-picker
        v-model="exportTime"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        @change="dateChange"
      >
      </el-date-picker>
      <el-tooltip
        class="item"
        effect="dark"
        content="时间范围只能选择两个月之内"
        placement="top"
      >
        <i class="el-icon-question"></i>
      </el-tooltip>
      <p class="exptitle" style="padding-top: 10px">用户</p>
      <change-user-input
        ref="changeUserInput"
        @sendValue="
          (data) => {
            export_uid = data.info.user_id;
          }
        "
        @clear="export_uid = ''"
      ></change-user-input>

      <div slot="footer" class="dialog-footer">
        <el-button @click="exportbuVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleExport" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  get_coupons_getLog,
  get_blank_out,
  exportLog,
} from "@/api/marketing/marketing_coupon.js";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import dateOption from "@/options/dateOption";
import { parseTime, exportMethod, getNowMonth } from "@/utils/index";

export default {
  components: {},
  data() {
    return {
      // 时间
      parseTime,
      exportMethod,
      getNowMonth,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: [],
      createTime: [],
      pickerOptions: dateOption,
      listLoading: false,
      // 搜索
      form: {
        status: "",
        uid: "",
        start_time: "",
        end_time: "",
        id: "",
      },
      typeList: [],
      status_list: [],
      // 全选
      selectArr: [],
      // 导出时间
      exportTime: "",
      selectname: "",
      exportTime: [],
      export_uid: "",
      exportbuVisible: false,
    };
  },
  created() {
    if (this.$route.params.id) {
      this.form.id = this.$route.params.id;
    }
    // 列表
    this.get_coupons_getLog();
  },
  methods: {
    //获取用户信息
    getUser(data) {
      let uid = data.info.user_id;
      this.form.uid = uid;
      this.get_coupons_getLog();
    },
    // 清除用户的uid
    clearUid() {
      this.form.uid = "";
    },

    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.get_coupons_getLog();
    },
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.$refs["changeUserInput"].name = "";
      this.createTime = [];
      this.get_coupons_getLog();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.get_coupons_getLog();
    },
    changePage(page) {
      this.current.page = page;
      this.get_coupons_getLog();
    },
    // 列表
    get_coupons_getLog() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      get_coupons_getLog(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data.list.data;
          this.totalCount = res.data.list.total;
          this.status_list = res.data.status_list;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.createTime[0] ? this.createTime[0] : "";
      form.end_time = this.createTime[1] ? this.createTime[1] : "";
      return form;
    },
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push(item.id);
      });
    },

    handleCancel(row) {
      let name = row.member.nickname;
      let id = row.id;
      if (row.status != 0) {
        let status_text = this.status_list[row.status];
        this.$message.error(`【${status_text}】状态下无法作废`);
        return;
      }
      this.$confirm(`确定作废【${name}】的优惠券?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = Object.assign(
            {
              sign: Md5(
                objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
              ),
            },
            apiUrl().name,
            { id: id }
          );
          get_blank_out(data).then((res) => {
            if (res.errcode == 0) {
              this.$message.success("操作成功");
              this.get_coupons_getLog();
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
    // 合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        switch (index) {
          case 0:
            sums[index] = "合计";
            break;
        }
      });

      return sums;
    },
    // 导出
    handleExport() {
      if (this.exportTime.length == 0) {
        this.$message.warning("请选择需要导出的时间段");
        return;
      }
      let form = {
        start_time: "",
        end_time: "",
        uid: this.export_uid,
      };

      if (this.exportTime && this.exportTime.length > 0) {
        form.start_time = this.exportTime[0];
        form.end_time = this.exportTime[1];
      }
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, form)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form,
        { id: this.form.id }
      );
      exportLog(data).then((res) => {
        this.exportMethod(res, "优惠券发放记录");
      });
      this.exportbuVisible = false;
      this.$nextTick(() => {
        this.exportTime = [];
        this.uid = "";
      });
    },
    // 日期选择
    dateChange(val) {
      let start = val ? new Date(val[0]).getTime() : 0;
      let end = val ? new Date(val[1]).getTime() : 0;
      let diff = start - end;
      if (Math.abs(diff) > 3600 * 1000 * 24 * 60) {
        this.$message.warning("选择时间跨度不能超过两个月");
        this.exportTime = this.getNowMonth();
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 120px);
  }
}
// 导出订单表头
.exptitle {
  display: block;
  font-size: 16px;
  color: #000000;
  margin: 0 0 10px;
}
.company_name {
  padding-top: 15px;
}
.maintip {
  text-align: center;
  padding-top: 15px;
  font-size: 18px;
  span {
    display: inline;
    padding: 18px 0;
    color: red;
  }
}
.new_navbar_svg {
  vertical-align: top;
  margin-left: 5px;
  use {
    fill: #ffffff;
  }
}
.status0 {
  color: #03a98e;
}
.status1 {
  color: #118ab2;
}
.status2 {
  color: #909399;
}
.status3 {
  color: red;
}
// 导出订单表头
.exptitle {
  display: block;
  font-size: 16px;
  color: #000000;
  margin: 0 0 10px;
  cursor: pointer;
}
</style>
